﻿using BLL;
using System;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace Thksoft.ParameterSettings
{
    public partial class SetBank1 : BaseUI.BasePage
    {
        private BaseBLL bsBLL = new BaseBLL();
        private Sys_BankBLL bankBLL = new Sys_BankBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FORM_SEARCH.Text = "";
                //初始化绑定GridView
                BindData(true);
            }
        }

        #region 增，修，查方法

        /// <summary>
        /// 添加公司账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 查询公司账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            SelectMethod();
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        private void SelectMethod()
        {
            string where = "";
            if (FORM_SEARCH.Text.TrimEnd() != "")
            {
                if (IsNumeric(FORM_SEARCH.Text.TrimEnd()))
                {
                    where += "(N_ID = " + FORM_SEARCH.Text.TrimEnd();
                    where += " OR C_NAME LIKE '%" + FORM_SEARCH.Text.TrimEnd() + "%')";
                }
                else
                {
                    where += "C_NAME LIKE '%" + FORM_SEARCH.Text.TrimEnd() + "%'";
                }
            }
            else
            {
                where = null;
            }
            ViewState["WHERE"] = where;
            PageIng.CurrentPage = 1;
            BindData(false);
            //int count;
            //DataTable dt = bankBLL.GetData(where, null, 0, 1000, out count);
            //grvShow.DataSource = dt;
            //grvShow.DataBind();
        }

        public bool IsNumeric(string str) //接收一个string类型的参数,保存到str里
        {
            if (str == null || str.Length == 0)    //验证这个参数是否为空
                return false;                           //是，就返回False
            ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
            byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里

            foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是，就返回False
                }
            }
            return true;                                        //是，就返回True
        }

        /// <summary>
        /// 修改账号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                for (int i = 0; i < this.TABLE.Rows.Count; i++)
                {
                    if (((CheckBox)this.TABLE.Rows[i].FindControl("chkSelectId")).Checked == true)
                    {
                        count++;
                        if (count == 1)
                        {
                            string pid = ((CheckBox)TABLE.Rows[i].FindControl("chkSelectId")).ToolTip;
                            MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                            return;
                            //string url = "/Modal/EditBank.aspx?ID=" + pid;
                            //Response.Redirect(url);
                            //break;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        #endregion 增，修，查方法

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindData(false);
        }

        protected void BindData(bool isFirst)
        {
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
                ViewState["WHERE"] = null;
            }
            string where = ViewState["WHERE"] == null ? null : (string)ViewState["WHERE"];
            string sort = null;
            if (ViewState["SortOrder"] != null)
            {
                sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            }
            int count;
            DataTable dt = bankBLL.GetData(where, sort, (PageIng.CurrentPage - 1) * PageIng.RowsPerPage, PageIng.RowsPerPage, out count);
            PageIng.Visible = true;
            PageIng.RowsCount = count;
            PageIng.DataSource = dt;
            PageIng.GridViewSource = TABLE;
            PageIng.RowsPerPage = PageIng._rowsPerPage;
            PageIng.Bind();
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }
    }
}